package cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.tree.*;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.LayoutManager;
import java.awt.BorderLayout;

public class CatalogPanel extends TreePanel {
    public CatalogPanel(AbstractApplication theApp, LayoutManager layout, boolean isDoubleBuffered) {
        super(theApp, layout, isDoubleBuffered);
    }

    public CatalogPanel(AbstractApplication theApp, LayoutManager layout) {
        super(theApp, layout);
    }

    public CatalogPanel(AbstractApplication theApp, boolean isDoubleBuffered) {
        super(theApp, isDoubleBuffered);
    }

    public CatalogPanel(AbstractApplication theApp) {
        super(theApp);
    }

    @Override
    public void initial() {
        DataCatalog catalog = new DataCatalogManager4Postgresql().getRoot();

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TreeNodeData(catalog, catalog.getAlias(), true), true);

        //用根节点构建一个树模型
        TreeModel treeModel = new DefaultTreeModel(root);

        //用树模型构建一个JTree（如果直接将根节点传递给JTree构造器，那么这棵树就会自动构造一个默认的树模型）
        treeComponent = new CatalogTree(treeModel);
//        JTreeTools.createTree(treeComponent);

        //设置在JTree中显示的内容为TreeNodeData的text属性而不是整个TreeNodeData对象的toString表示
        //设置节点外观
        final TreeNodeRenderer renderer = new TreeNodeRenderer();
        treeComponent.setCellRenderer(renderer);

        final TreeNodeEditor editor = new TreeNodeEditor(treeComponent);
        treeComponent.setCellEditor(editor);

        //设置树节点可编辑
        treeComponent.setEditable(true);

        treeComponent.addTreeSelectionListener(new CatalogTreeSelectionListener(treeComponent, this.theApp));
        treeModel.addTreeModelListener(new CatalogTreeModelListener(treeComponent, this.theApp));
        treeComponent.addMouseListener(new CatalogTreeMouseListener(treeComponent, this.theApp));

        scrollPane = new JScrollPane(treeComponent);
        add(scrollPane, BorderLayout.CENTER);
    }

    @Override
    public void reset(Object o) {

    }
}
